/*
 * Copyright © 2019 iFund. All rights reserved.
 */

package com.climber.android.commonres.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import com.climber.android.commonres.R

/**
 * Created by mikes on 2018-5-9.
 */
enum class LoadMoreState{
    doing, done, error, noMore
}

class LoadMoreFooterView : LinearLayout {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet? = null) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    private val loadMoreDoingContainer : LinearLayout
    private val noMoreDataContainer : LinearLayout
    private val reloadMoreContainer : LinearLayout

    private var currentLoadMoreState : LoadMoreState?= null
    private var reloadListener : OnClickListener ?= null

    fun setReloadListener(listener : OnClickListener){
        reloadListener = listener
    }

    init {
        View.inflate(context, R.layout.com_view_loadmore_footer, this)

        loadMoreDoingContainer = findViewById(R.id.loadMoreDoingContainer)
        noMoreDataContainer = findViewById(R.id.noMoreDataContainer)
        reloadMoreContainer = findViewById(R.id.reloadMoreContainer)

        hideAllViews()
    }

    fun hideAllViews(){
        loadMoreDoingContainer.visibility = View.GONE
        noMoreDataContainer.visibility = View.GONE
        reloadMoreContainer.visibility = View.GONE
    }

    fun changeLoadMoreState(state : LoadMoreState){
        currentLoadMoreState = state
        when(state){
            LoadMoreState.doing ->{
                loadMoreDoingContainer.visibility = View.VISIBLE
                noMoreDataContainer.visibility = View.GONE
                reloadMoreContainer.visibility = View.GONE
            }
            LoadMoreState.done ->{
                loadMoreDoingContainer.visibility = View.GONE
                noMoreDataContainer.visibility = View.GONE
                reloadMoreContainer.visibility = View.GONE
            }
            LoadMoreState.error ->{
                loadMoreDoingContainer.visibility = View.GONE
                noMoreDataContainer.visibility = View.GONE
                reloadMoreContainer.visibility = View.VISIBLE

                reloadMoreContainer.setOnClickListener { view ->
                    reloadListener?.onClick(view)
                }
            }
            LoadMoreState.noMore ->{
                loadMoreDoingContainer.visibility = View.GONE
                noMoreDataContainer.visibility = View.VISIBLE
                reloadMoreContainer.visibility = View.GONE
            }
        }
    }

    fun getCurrentLoadMoreState() = currentLoadMoreState
}